package com.xms.form.api.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.xms.form.core.FormType;
import com.xms.form.entity.SystemForm;
import com.xms.form.service.ISystemFormFinderService;
import com.xms.form.service.ISystemFormUpdaterService;
import com.xms.schema.entity.Entity;
import com.xms.schema.service.entity.IEntityFinderService;
import com.xms.security.core.PrivilegeResourceItem;
import com.xms.utils.CollectionUtil;
import com.xms.web.framework.controller.ApiBaseController;
import com.xms.web.framework.model.JsonResultObject;
import com.xms.web.framework.model.UpdateAuthorizationStateModel;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * SystemFormAuthorizationController
 * </p>
 *
 * @author migo
 * @since 2021/10/20
 */
@RestController
@RequestMapping("{org}/api/schema/form")
public class SystemFormAuthorizationController extends ApiBaseController {
    private ISystemFormFinderService _systemFormFinderService;
    private ISystemFormUpdaterService _systemFormUpdaterService;
    private IEntityFinderService _entityFinderService;

    @Autowired
    public SystemFormAuthorizationController(ISystemFormFinderService systemFormFinderService,
                                             ISystemFormUpdaterService systemFormUpdaterService,
                                             IEntityFinderService entityFinderService) {
        _systemFormFinderService = systemFormFinderService;
        _systemFormUpdaterService = systemFormUpdaterService;
        _entityFinderService = entityFinderService;
    }

    @ApiOperation("查询表单权限资源")
    @GetMapping("privilegeresource")
    public JsonResultObject privilegeResource(@RequestParam(value = "authorizationenabled", required = false) Boolean authorizationEnabled) {
        LambdaQueryWrapper<SystemForm> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SystemForm::getStatecode, true)
                .eq(SystemForm::getIsdefault, false)
                .eq(SystemForm::getFormtype, FormType.Main);
        if (authorizationEnabled != null) {
            queryWrapper.eq(SystemForm::getAuthorizationenabled, authorizationEnabled);
        }
        queryWrapper.select(SystemForm::getSystemformid, SystemForm::getName, SystemForm::getEntityid, SystemForm::getAuthorizationenabled);
        List<SystemForm> data = _systemFormFinderService.list(queryWrapper);
        if (CollectionUtil.notEmpty(data)) {
            List<PrivilegeResourceItem> result = new ArrayList<>();
            List<Entity> entities = _entityFinderService.all();
            for (Entity item : entities) {
                List<SystemForm> forms = data.stream().filter(x -> x.getEntityid().equals(item.getEntityid())).collect(Collectors.toList());
                if (CollectionUtil.isEmpty(forms)) {
                    continue;
                }
                PrivilegeResourceItem group1 = new PrivilegeResourceItem() {
                    {
                        label = item.getLocalizedname();
                        children = forms.stream().map(x -> (new PrivilegeResourceItem() {{
                            id = x.getSystemformid();
                            label = x.getName();
                            authorizationenabled = x.getAuthorizationenabled();
                        }})).collect(Collectors.toList());
                    }
                };
                result.add(group1);
            }
            return JOk(result);
        }
        return JOk();
    }

    @ApiOperation("启用表单权限")
    @RequestMapping(value = "authorizationenabled", method = RequestMethod.POST)
    public JsonResultObject authorizationEnabled(@RequestBody UpdateAuthorizationStateModel model) {
        List<SystemForm> authorizations = _systemFormFinderService.getAuthorizations();
        if (CollectionUtil.notEmpty(authorizations)) {
            _systemFormUpdaterService.updateAuthorization(false, authorizations.stream().map(x -> x.getSystemformid()).collect(Collectors.toList()));
        }
        if (CollectionUtil.notEmpty(model.objectId)) {
            _systemFormUpdaterService.updateAuthorization(true, model.objectId);
        }
        return JSaveSuccess();
    }
}
